/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.front;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.service.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/7/24 22:46
 * 接受前台发送的留言
 */
@Controller
public class FrontMessage {
    CqLogger logger = CqLogger.getInstance();

    private final MessageService messageService;

    public FrontMessage (MessageService messageService)
    {
        this.messageService = messageService;
    }

    @PostMapping("api/sendMessage")
    public void getReportIP(HttpServletRequest request, @RequestBody Map<String, String> requestMap) {
        String ip;
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        }
        else {
            ip = request.getHeader("x-forwarded-for");
        }
        logger.print(ip);
        String name = requestMap.get("name");
        String email = requestMap.get("email");
        String phone = requestMap.get("phone");
        String message = requestMap.get("message");

        messageService.sendMessage(message, email, phone, name, ip);
    }
}
